#include "gamecontrol.h"

GameControl* GameControl :: instance = nullptr;

GameControl::GameControl(QWidget* parent) : QObject(parent)
{
    //添加平台
    //顶层
    addPlatform(40,10,1250,40);
    //第一层
    addPlatform(280,150,150,30);
    addPlatform(380,180,100,30);
    addPlatform(380,210,170,40);
    addPlatform(380,250,170,70);
    addPlatform(550,210,370,40);
    addPlatform(925,180,100,70);
    addPlatform(1030,210,250,40);
    //第二层
    addPlatform(40,250,141,100);
    addPlatform(40,380,610,40);
    addPlatform(650,330,300,90);
    addPlatform(980,380,190,80);
    //第三层
    addPlatform(170,520,480,40);
    addPlatform(650,520,70,60);
    addPlatform(720,550,371,40);
    addPlatform(1100,550,140,70);
    addPlatform(1230,550,71,110);
    //第四层
    addPlatform(40,690,540,40);
    addPlatform(580,690,60,100);
    addPlatform(640,750,201,40);
    addPlatform(790,790,101,30);
    addPlatform(970,750,171,40);
    //四层半
    addPlatform(40,820,400,30);
    //第五层
    addPlatform(40,955,571,30);
    addPlatform(790,955,100,30);
    addPlatform(1060,955,140,30);
    addPlatform(1200,860,141,90);
    //小平台
    addPlatform(40,620,90,20);

    //添加墙
    //第五层
    addWallLeft(195,250,10,130);
    addWallLeft(422,150,8,31);
    addWallLeft(475,180,8,31);
    addWallLeft(1020,180,10,30);
    addWallLeft(540,250,8,70);
    addWallLeft(375,190,5,121);
    //第四层
    addWallLeft(955,330,8,41);
    //第三层
    addWallLeft(712,520,8,31);
    //第二层
    addWallLeft(634,690,8,61);
    addWallLeft(880,790,8,31);
    //小平台
    addWallLeft(125,640,10,40);

    //第五层
    addWallRight(375,190,5,120);
    addWallRight(920,180,8,30);
    //第四层
    addWallRight(650,330,5,51);
    addWallRight(980,425,5,31);
    //第三层
    addWallRight(650,555,5,31);
    addWallRight(1100,590,5,31);
    addWallRight(1239,620,5,41);
    //第二层
    addWallRight(580,720,8,70);
    addWallRight(790,790,8,31);
    //第一层
    addWallRight(1195,860,5,91);
    //右边界
    addWallRight(1300,10,40,845);
}

void GameControl::GameInit()
{
    //设置视图的父亲为窗口
    mGameView.setParent(Widget::widget);

    LoadStartScene();//开始场景初始化

    //定时器初始化
    mPlayerMoveTimer = new QTimer(this);
    mJumpTimer = new QTimer(this);
    mFormTimer = new QTimer(this);
    mGameTimer = new QTimer(this);

    lcdnumber = new QLCDNumber;

    connect(mPlayerMoveTimer,&QTimer::timeout,this,[this]()
            {
                mManBlue.Move();
                mManBlue.changePixmap();
                mManBlue.diamondCollisions();

                mManRed.Move();
                mManRed.changePixmap();
                mManRed.diamondCollisions();

                mStone.Move();
                mSwitchcross.move();
            });

    connect(mJumpTimer,&QTimer::timeout,this,[this]()
            {
                mManBlue.updatePosition();
                mManBlue.stoneCollisions();
                mManBlue.strategyCollision();

                mManRed.updatePosition();
                mManRed.stoneCollisions();
                mManRed.strategyCollision();

                mStrategyUpdown.collision();
            });

    connect(mFormTimer,&QTimer::timeout,this,[this]()
            {
                mManBlue.detectCollisions();

                mManRed.detectCollisions();

                mSwitchcross.collision();

            });


    connect(mPlayerMoveTimer,&QTimer::timeout,this,&GameControl::GameOver);

    connect(mGameTimer,&QTimer::timeout,this,[this]()
            {
                static int num =180;

                if(num > 0)
                {
                    num--;
                    lcdnumber->display(num);
                }
                else
                {
                    QMessageBox::critical(nullptr,"时间结束","请重新开始");
                    mKeyList.removeAll(mKeyList.size());
                    mManBlue.setPos(20,879);
                    mManRed.setPos(70,879);
                    num = 180;
                    lcdnumber->display(num);
                }

            });

}

void GameControl::LoadStartScene()
{
    mStartScene.setSceneRect(QRect(0,0,1347,1009));
    mStartScene.addPixmap(QPixmap(":/1/Image/BackgroundStart.png"));

    auto startbtn = new QToolButton;
    startbtn->setAutoRaise(true);
    startbtn->setIcon(QIcon(":/1/Image/ButtonStart.png"));
    startbtn->setIconSize(QSize(353,46));
    startbtn->move(470,500);

    auto startlabel = new QLabel;
    startlabel->setText("使用WASD移动冰人，使用5123移动火人");
    startlabel->setFont(QFont("",20,1));
    startlabel->move(330,750);

    //开始游戏点击
    connect(startbtn,&QToolButton::clicked,this,[this](){

        //加载游戏场景
        this->LoadGameScene();
        //开始游戏
        this->GameStart();
    });

    mStartScene.addWidget(startbtn);
    mStartScene.addWidget(startlabel);

    //设置当前场景为开始场景
    mGameView.setScene(&mStartScene);
    mGameView.show();
}

void GameControl::LoadGameScene()
{
    mGameScene.setSceneRect(QRect(0,0,1347,1009));

    //设置坐标
    mGameScene.setSceneRect(QRect(0,0,1347,1009));
    mWaterRed.setPos(620,937);
    mWaterBlue.setPos(890,907);
    mWaterGreen.setPos(840,715);
    mStrategyCross.setPos(30,490);
    mDoorBlue.setPos(1220,140);
    mDoorRed .setPos(1140,140);

    lcdnumber->setGeometry(590,5,160,40);
    lcdnumber->display(180);

    //添加图片
    mBackground.setPixmap(QPixmap(":/1/Image/Background.png"));
    mWaterRed.setPixmap(QPixmap(":/1/Image/WaterRed.png"));
    mWaterBlue.setPixmap(QPixmap(":/1/Image/WaterBlue.png"));
    mWaterGreen.setPixmap(QPixmap(":/1/Image/WaterGreen.png"));
    mStrategyCross.setPixmap(QPixmap(":/1/Image/StrategyCross.png"));
    mDoorBlue.setPixmap(QPixmap(":/1/Image/DoorBlue4.png"));
    mDoorRed.setPixmap(QPixmap(":/1/Image/DoorRed4.png"));

    //图片元素添加到场景
    mGameScene.addItem(&mBackground);
    mGameScene.addItem(&mWaterRed);
    mGameScene.addItem(&mWaterBlue);
    mGameScene.addItem(&mWaterGreen);
    mGameScene.addItem(&mStrategyCross);
    mGameScene.addItem(&mStrategyUpdown);
    mGameScene.addItem(&mSwitchcross);
    mGameScene.addItem(&mDoorBlue);
    mGameScene.addItem(&mDoorRed);

    mGameScene.addWidget(lcdnumber);


    //钻石创建
    QPixmap DiamondImageBlue(":/1/Image/DiamondBlue.png");
    QPixmap DiamondImageRed(":/1/Image/DiamondRed.png");

    QPoint pos1(820,150);
    QPoint pos2(90,180);
    QPoint pos3(830,480);
    QPoint pos4(950,880);

    QPoint pos5(230,460);
    QPoint pos6(310,90);
    QPoint pos7(570,150);
    QPoint pos8(670,895);

    Diamond* diamond1 = new Diamond(pos1,DiamondImageBlue,Diamond::DT_Blue);
    Diamond* diamond2 = new Diamond(pos2,DiamondImageBlue,Diamond::DT_Blue);
    Diamond* diamond3 = new Diamond(pos3,DiamondImageBlue,Diamond::DT_Blue);
    Diamond* diamond4 = new Diamond(pos4,DiamondImageBlue,Diamond::DT_Blue);

    Diamond* diamond5 = new Diamond(pos5,DiamondImageRed,Diamond::Dt_Red);
    Diamond* diamond6 = new Diamond(pos6,DiamondImageRed,Diamond::Dt_Red);
    Diamond* diamond7 = new Diamond(pos7,DiamondImageRed,Diamond::Dt_Red);
    Diamond* diamond8 = new Diamond(pos8,DiamondImageRed,Diamond::Dt_Red);

    mGameScene.addItem(diamond1);
    mGameScene.addItem(diamond2);
    mGameScene.addItem(diamond3);
    mGameScene.addItem(diamond4);
    mGameScene.addItem(diamond5);
    mGameScene.addItem(diamond6);
    mGameScene.addItem(diamond7);
    mGameScene.addItem(diamond8);

    mDiamondsBlue.append(diamond1);
    mDiamondsBlue.append(diamond2);
    mDiamondsBlue.append(diamond3);
    mDiamondsBlue.append(diamond4);

    mDiamondsRed.append(diamond5);
    mDiamondsRed.append(diamond6);
    mDiamondsRed.append(diamond7);
    mDiamondsRed.append(diamond8);

    //上下按钮创建
    QPoint _pos1(390,505);
    QPoint _pos2(1010,370);

    SwitchUpdown* switchupdown1 = new SwitchUpdown(_pos1);
    SwitchUpdown* switchupdown2 = new SwitchUpdown(_pos2);

    mGameScene.addItem(switchupdown1);
    mGameScene.addItem(switchupdown2);

    mSwitchUpdown.append(switchupdown1);
    mSwitchUpdown.append(switchupdown2);

    p1 = &mManBlue;
    p1 = &mManRed;
    p1 = &mStone;

    mGameScene.addItem(&mManRed);
    mGameScene.addItem(&mManBlue);
    mGameScene.addItem(&mStone);

    //设置当前场景为游戏场景
    mGameView.setScene(&mGameScene);
    mGameView.show();


}

void GameControl::GameStart()//游戏开始
{
    //开始定时器
    mPlayerMoveTimer->start(GameDefine::playerMoveTimer);
    mJumpTimer->start(GameDefine::playerMoveTimer);
    mFormTimer->start(GameDefine::platformTimer);
    mGameTimer->start(GameDefine::gameTimer);
}

void GameControl::GameOver()//游戏结束
{
    this->setParent(Widget::widget);
    QRectF mManBlueRect = mManBlue.boundingRect().translated(mManBlue.pos());
    mManBlueRect = QRectF(mManBlue.x(),mManBlue.y(),25,25);

    QRectF mManRedRect = mManRed.boundingRect().translated(mManRed.pos());
    mManRedRect = QRectF(mManRed.x(),mManRed.y(),25,25);

    QRectF mDoorBlueRect = mDoorBlue.boundingRect().translated(mDoorBlue.pos());
    QRectF mDoorRedRect = mDoorRed.boundingRect().translated(mDoorRed.pos());
    if( mManBlueRect.intersects(mDoorBlueRect) &&mManRedRect.intersects(mDoorRedRect))
    {
        mSuccessScene.addPixmap(QPixmap(":/1/Image/Success.png"));
        mGameView.setScene(&mSuccessScene);
        mPlayerMoveTimer->stop();
        mFormTimer->stop();
        mGameTimer->stop();
        mGameView.show();
    }
}

void GameControl::addPlatform(double x,double y,int width,int height)//添加平台到场景
{
    mGameScene.addRect(x,y,width,height);
    platforms.append(QRectF(x,y,width,height));
}

void GameControl::addWallLeft(double x,double y,int width,int height)
{
    mGameScene.addRect(x,y,width,height);
    wallsLeft.append(QRectF(x,y,width,height));
}

void GameControl::addWallRight(double x,double y,int width,int height)
{
    mGameScene.addRect(x,y,width,height);
    wallsRight.append(QRectF(x,y,width,height));
}

QGraphicsScene& GameControl::getGameScene()
{
    return mGameScene;
}

QGraphicsItem& GameControl::getmStone()
{
    return mStone;
}

QGraphicsItem& GameControl::getManBlue()
{
    return mManBlue;
}

QGraphicsItem& GameControl::getManRed()
{
    return mManRed;
}

QGraphicsItem& GameControl::getmStrategyCross()
{
    return mStrategyCross;
}

QGraphicsItem& GameControl::getStrategyUpdown()
{
    return mStrategyUpdown;
}
